<?php
//add dragon.hu 2016.1.6 businesschargedata

defined('BASEPATH') or die('forbidden');

class Businesschargedata_Controller extends Controller
{
	//private $accountconn;
	private $areaList;
	private $startdate;
	private $channel;
	private $channelList;  //渠道列表
	//private $areanum;
	private $opentime;  //开服时间
	private $logconn;
	//private $conn;
	private $linkInfoList;  //所有连接的信息
	private $inc;
	private $paydb;  
	
	function _init()
	{
		check_level(50, segment(0), segment(1));
		set_time_limit(0);
		
		//$this->areanum = $_SESSION['area_selected_id'];
		//if ($this->areanum == '')
		//{
		//	echo "<script>alert('".translate('C_SELECT_AREA_TEXT')."');</script>";
		//	return;
		//}
		
		/*$this->channelList = array(
				'0' => '渠道无关', 'uc' => 'UC', 'baidu' => 'Baidu', 'sanliuling' => '360',
				'xiaomi' => 'Xiaomi', 'lianxiang' => 'Lianxiang', 'oppo' => 'Oppo', 'huawei' => 'Huawei'
		);*/
		$this->inc = $this->config['inc'];
		$this->channelList = $this->inc['allchannel'];
		array_push($this->channelList, '渠道无关');
		/*$hostlist = get_area_list_detal();
		//检查配置了哪些数据库
		$sql = "SELECT areanum FROM server_mapping";
		$this->db->query($sql);
		$tmpdatabase = array();
		while($n = $this->db->get_array('default'))
		{
			$tmpdatabase[] = $n['areanum'];
		}
		$tmpdatabase = array_unique($tmpdatabase);
		//所有注册的服务器
		$tmpallserver = array();
		foreach ($hostlist as $k=>$v)
		{
			$tmpallserver[] = $k;
		}
		$targetarea = array_intersect($tmpdatabase, $tmpallserver);
		
		foreach ($targetarea as $k=>$v)
		{
			$this->linkInfoList[$v]['opentime'] = $hostlist[$v]['opentime'];
			$this->linkInfoList[$v]['conn'] = getTmpDbConnect(PLATFORM_ALL, 'all', $v, DBTYPE_GAMESERVER);
		}*/
		//$this->accountconn = $this->load->database('center_db');
		//$this->conn = getTmpDbConnect(PLATFORM_ALL, 'all', $this->areanum, DBTYPE_GAMESERVER);
		//$this->logconn = getTmpDbConnect(PLATFORM_ALL, 'all', $this->areanum, DBTYPE_LOG);
		$this->logconn = $this->load->database('log_db');  //帐号服上的log库
		$this->paydb = $this->load->database('payment_db');  //支付log
		//$this->conn = $this->load->database('log_db');
	}
	
	function index()
	{
		if(is_submit())
		{
			$this->startdate = $_POST['row']['starttime'];
			//$enddate = $_POST['row']['endtime'];
			//$serveridtmp = $_POST['row']['area'];
			//$tmp = explode("-", $serveridtmp);
			//$serverid = $tmp[0];
			$this->channel = $_POST['row']['channel'];
			//var_dump($_POST);
			//exit();
			$areaIds = $_POST['areaIds'];
		}else
		{
			//$this->startdate = date("Y-m-d", $this->opentime);  //默认开服时间
			$opentimestamp = 0;
			$i = 0;
			/*foreach ($this->linkInfoList as $k=>$v)  //默认开服时间最早的
			{
				if ($v['opentime'] == 0)
				{
					continue;  //过滤时间戳为0的
				}
				if($i == 0){
					$opentimestamp = $v['opentime'];
					$i++;
					continue;
				}
				if ($v['opentime'] < $opentimestamp)
				{
					$opentimestamp = $v['opentime'];
				}
			}*/
			$this->startdate = date("Y-m-d", /*$this->linkInfoList[$this->areanum]['opentime'] $opentimestamp*/time());
			$this->channel = '0';  //默认渠道无关
			$areaIds = array(0);  //默认所有服
			//$serverid = $this->areanum;
		}
		
		if (empty($this->logconn))
		{
			echo "<script>alert('connect database failed!!!');</script>";
			return;
		}
		
		$remainData = array();
		if (isset($this->startdate))
		{
			//$tmpdate = $this->startdate;
			$tmpdate = date("Y-m-d", strtotime($this->startdate));
			/*//1.当日流水：当日玩家充值总数（针对账号）
			//echo "aaaaa";
			//$sql1 = "SELECT SUM(money) AS num1 FROM player_payment WHERE finish_time like '".$tmpdate."%'";
			$this->channel == '0' ? $sql1 = "SELECT SUM(money) AS num1 FROM player_payment WHERE finish_time >'".$this->startdate."' AND finish_time <'".$enddate."' AND channel != 'shzmTest'" : $sql1 = "SELECT SUM(money) AS num1 FROM player_payment WHERE finish_time > '".$this->startdate."' AND finish_time <'".$enddate."' and channel = '".$this->channel."'";
			//var_dump($this->channel == '0');
			//var_dump($sql);
			//$one1 = $this->conn->one($sql1);
			$allmoney = 0;
			if (in_array('0', $areaIds))
			{
				//$tmplist = get_area_list_option();
				foreach ($this->linkInfoList as $k=>$v)
				{
					$one1 = $v['conn']->one($sql1);
					$allmoney += $one1['num1'];
				}
			}
			else
			{
				foreach ($areaIds as $k=>$v)
				{
					if (isset($this->linkInfoList[$v]))
					{
						$one1 = $this->linkInfoList[$v]['conn']->one($sql1);
						$allmoney += $one1['num1'];
					}
				}
			}*/
			$this->channel == '0' ? $sql1 = "SELECT SUM(money) AS num1 FROM tt_payment WHERE finish_time like '$tmpdate%' AND channel != 'shzmTest' AND status = 1 AND serverid != 375" : $sql1 = "SELECT SUM(money) AS num1 FROM tt_payment WHERE finish_time like '$tmpdate%' AND channel = '$this->channel' AND status = 1 AND serverid != 375";
			$one1 = $this->paydb->one($sql1);
			$allmoney = $one1['num1'];
			
			$remainData[$tmpdate]['dayrunning'] = /*$one1['num1']*/round($allmoney, 2);
				
			//今日注册账号数组
			//$sql = "SELECT rolecreate.role_id FROM rolecreate where operation_time like '".$tmpdate.'%'."'";
			//$sql = "SELECT account FROM accountcreate WHERE operation_time like '".$tmpdate."%'";
			$this->channel == '0' ? $sql = "SELECT account FROM accountcreate WHERE operation_time like '$tmpdate%' AND platform_id != 'PCChannel'" : $sql = "SELECT account FROM accountcreate WHERE operation_time like '$tmpdate%' and platform_id = '".$this->channel."'";
			
			$this->logconn->query($sql);
			$regAccountList = array();
			while ($n = $this->logconn->get_array('default'))
			{
				$regAccountList[] = $n['account'];
			}
			
			/*//2.付费人数：当日玩家付费总人数（根据账号）
			//echo "bbbbb";
			//$sql2 = "SELECT DISTINCT(roleid) FROM player_payment WHERE finish_time like '".$tmpdate.'%'."'";
			//$sql2 = "SELECT player.accountid, SUM(player_payment.money) AS allnum FROM player_payment LEFT JOIN player on player_payment.roleid = player.id WHERE player_payment.finish_time like '".$tmpdate."%' GROUP BY player.accountid";
			$this->channel == '0' ? $sql2 = "SELECT player.accountid, SUM(player_payment.money) AS allnum FROM player_payment LEFT JOIN player on player_payment.roleid = player.id WHERE player_payment.finish_time > '".$this->startdate."' and finish_time < '".$enddate."' AND player_payment.channel != 'shzmTest' GROUP BY player.accountid" : $sql2 = "SELECT player.accountid, SUM(player_payment.money) AS allnum FROM player_payment LEFT JOIN player on player_payment.roleid = player.id WHERE player_payment.finish_time > '".$this->startdate."' AND player_payment.finish_time < '".$enddate."' AND player_payment.channel = '".$this->channel."' GROUP BY player.accountid";

			//$this->conn->query($sql2);
			$payAccountList = array();  //不包括付费数量
			$payAccountAllInfo = array();  //所有服信息
			$payAccountListExt = array();  //包括付费数量
			
			if (in_array('0', $areaIds))
			{
				//$tmplist = get_area_list_option();
				foreach ($this->linkInfoList as $k=>$v)
				{
					$v['conn']->query($sql2);
					//
					while ($n = $v['conn']->get_array('default'))
					{
						$payAccountList[] = $n['accountid'];
						$payAccountAllInfo[$n['accountid']][$k] = $n;
					}
				}
			}
			else
			{
				foreach ($areaIds as $k=>$v)
				{
					if (isset($this->linkInfoList[$v]))
					{
						$this->linkInfoList[$v]['conn']->query($sql2);
						//
						while ($n = $this->linkInfoList[$v]['conn']->get_array('default'))
						{
							$payAccountList[] = $n['accountid'];
							$payAccountAllInfo[$n['accountid']][$v] = $n;
						}	
					}
				}
			}
			$payAccountList = array_unique($payAccountList);

			foreach ($payAccountAllInfo as $k=>$v)
			{
				foreach ($v as $k1=>$v1)
				{
					$payAccountListExt[$k] += $v1['allnum'];
				}
			}
			//while ($n = $this->conn->get_array('default'))
			//{
			//	$payAccountList[] = $n['accountid'];
			//	$payAccountListExt[$n['accountid']] = $n;
			//}
			//var_dump($payAccountList);
			//exit();*/
			$payAccountList = array();  //不包括付费数量
			//$payAccountAllInfo = array();  //所有服信息
			$payAccountListExt = array();  //包括付费数量
			
			
			$this->channel == '0' ? $sql2 = "SELECT accountid, SUM(money) AS allnum FROM tt_payment WHERE finish_time like '$tmpdate%' AND channel != 'shzmTest' AND status = 1 GROUP BY accountid" : $sql2 = "SELECT DISTINCT(accountid) FROM tt_payment WHERE finish_time like '$tmpdate%' AND channel = '$this->channel' AND status = 1 GROUP BY accountid";
			$this->paydb->query($sql2);
			while($n = $this->paydb->get_array())
			{
				$payAccountList[] = $n['accountid'];
				$payAccountListExt[$n['accountid']] = $n['allnum'];
			}
			$remainData[$tmpdate]['dayconsumer'] = count($payAccountList);
			
			//3.活跃付费率：当日付费玩家总数/当日登陆玩家总数
			//echo "ccccc";
			//$sql3 = "SELECT COUNT(DISTINCT(role_id)) AS num3 FROM rolelogin WHERE operation_time like '".$tmpdate.'%'."'";
			//$sql3 = "SELECT COUNT(DISTINCT(account_id)) AS num3 FROM accountlogin WHERE login_time like '".$tmpdate."%'";
			$this->channel == '0' ? $sql3 = "SELECT COUNT(DISTINCT(account_id)) AS num3 FROM accountlogin WHERE login_time like '$tmpdate%' AND platform_id != 'PCChannel'" : $sql3 = "SELECT COUNT(DISTINCT(account_id)) AS num3 FROM accountlogin WHERE login_time like '$tmpdate%' AND platform_id ='".$this->channel."'";
			$one3 = $this->logconn->one($sql3);
			//var_dump($one3);
			//exit();
			$remainData[$tmpdate]['activepayrate'] = round(count($payAccountList) / $one3['num3'], 4) * 100 . "%";
				
			//4.新增付费率：新增用户中付费人数/当日新增用户
			//echo "ddddd";
			$tmparr = array_intersect($regAccountList, $payAccountList);
				
			$remainData[$tmpdate]['addpayrate'] = round(count($tmparr) / count($regAccountList), 4) * 100 . "%";
				
			//5.注册arpu ：截止到当日，总收入/总注册人数
			/*//echo "eeeee";
			//$tmpsql = "SELECT SUM(money) AS num FROM player_payment WHERE finish_time > '".$this->startdate."' AND finish_time < '".$tmpdate." 23:59:59'";
			$this->channel == '0' ? $tmpsql = "SELECT SUM(money) AS num FROM player_payment WHERE finish_time > '".$this->startdate."' AND finish_time < '".$enddate."' AND channel != 'shzmTest'" : $tmpsql = "SELECT SUM(money) AS num FROM player_payment WHERE finish_time > '".$this->startdate."' AND finish_time < '".$enddate."' AND channel = '".$this->channel."'";
			//$untilincome = $this->conn->one($tmpsql);
			$untilincome = 0;
			if (in_array('0', $areaIds))
			{
				//$tmplist = get_area_list_option();
				foreach ($this->linkInfoList as $k=>$v)
				{
					$tmpincome = $v['conn']->one($tmpsql);
					$untilincome += $tmpincome['num'];
				}
			}
			else
			{
				foreach ($areaIds as $k=>$v)
				{
					if (isset($this->linkInfoList[$v]))
					{
						$tmpincome = $this->linkInfoList[$v]['conn']->one($tmpsql);
						$untilincome += $tmpincome['num'];
					}
				}
			}
				
			//$sql = "SELECT COUNT(DISTINCT(role_id)) AS num FROM rolecreate WHERE operation_time > '".$this->startdate."' AND operation_time < '".$tmpdate." 23:59:59'";
			//$sql = "SELECT COUNT(DISTINCT(account)) AS num FROM accountcreate WHERE operation_time > '".$this->startdate."' AND operation_time < '".$tmpdate." 23:59:59'";*/
			$this->channel == '0' ? $sql = "SELECT COUNT(DISTINCT(account)) AS num FROM accountcreate WHERE operation_time < '$tmpdate 23:59:59' AND platform_id != 'PCChannel'" : $sql = "SELECT COUNT(DISTINCT(account)) AS num FROM accountcreate WHERE operation_time < '$tmpdate 23:59:59' AND platform_id = '".$this->channel."'";
			$untilregister = $this->logconn->one($sql);
			$this->channel == '0' ? $tmpsql = "SELECT SUM(money) AS num FROM tt_payment WHERE finish_time < '$tmpdate 23:59:59' AND channel != 'shzmTest' AND status = 1" : $tmpsql = "SELECT SUM(money) AS num FROM tt_payment WHERE finish_time < '$tmpdate%' AND channel = '".$this->channel."' AND status = 1";
			$tmpone = $this->paydb->one($tmpsql);
			$remainData[$tmpdate]['registerarpu'] = round($tmpone['num'] / $untilregister['num'], 2);
				
			//6.付费arpu ：当日收入/ 当天付费人数
			//echo "fffff";
			$remainData[$tmpdate]['payarpu'] = round((/*$one1['num1']*/$allmoney / count($payAccountList)), 2);
				
			//7.登陆arpu ： 当天收入/当天登陆人数
			//echo "ggggg";
			$remainData[$tmpdate]['loginarpu'] = round((/*$one1['num1']*/$allmoney / $one3['num3']), 2);
				
			//8.新增arpu ：当天新增用户带来的收入/当天注册人数
			//echo "hhhhh";
			//SELECT SUM(money) FROM player_payment  LEFT JOIN player on player_payment.roleid = player.id WHERE player_payment.finish_time LIKE '2015-12-18%' AND player.createtime LIKE '2015-12-18%';
			//$sql4 = "SELECT SUM(player_payment.money) AS num FROM player_payment LEFT JOIN player on player_payment.roleid = player.id WHERE player_payment.finish_time LIKE '".$tmpdate."%' AND player.createtime LIKE '".$tmpdate."%'";
			//$one4 = $this->conn->one($sql4);
			//新增用户带来的收入
			$totalmoney = 0;
			//for ($j = 0; $j < count($tmparr); $j++)
			//{
			//	$totalmoney += $payAccountListExt[$tmparr[$j]]['allnum'];
			//}
			foreach ($tmparr as $k => $v)
			{
				$totalmoney += $payAccountListExt[$v]/*['allnum']*/;
			}
			
			$remainData[$tmpdate]['addarpu'] = round($totalmoney / count($regAccountList), 2);
		}
		
		//var_dump($remainData);
		//exit();
		
		set_area_list_view("arealist");
		$this->view->assign('startdate', $tmpdate);
		//$this->view->assign('enddate', $enddate);
		//$this->view->assign('areaList', $this->areaList);
		$this->view->assign ("channel", $this->form->select ("row[channel]", $this->channelList, '', array('id' => 'channel')));
		$this->view->assign('remainData', $remainData);
		
		$this->view->display("businessdata/businesschargedata/index");
	}
	
}